/**这个文件用来学习使用 pinia */

//导入defineStore函数，用于定义store
import { defineStore } from "pinia";
import { ref } from "vue";
import type { Ref } from "vue";

//定义返回类型
interface CounterStore {
  count: Ref<number>;
  addCount: () => void;
  subCount: () => void;
}

//这个useCountStore 是一个函数，调用它会返回一个store对象
export const useCountStore = defineStore("counter", (): CounterStore => {
  //1.定义数据
  const count = ref<number>(0);

  //2.定义操作数据的方法
  const addCount = (): void => {
    count.value++;
  };

  const subCount = (): void => {
    count.value--;
  };

  //3.以对象的形式将数据和函数导出去
  return { count, addCount, subCount };
});
